from fastapi import FastAPI ,Request
from fastapi.middleware.cors import CORSMiddleware
from utils.database import create_tables  # 导入数据库相关函数
from routers import user  # 导入用户路由
from routers import match  # 导入用户路由
from utils.background_tasks import start_background_tasks  # 导入后台任务启动函数
from starlette.templating import Jinja2Templates
import logging
import uvicorn
from fastapi.staticfiles import StaticFiles
app = FastAPI()

templates = Jinja2Templates(directory="templates")

# 包含用户路由
app.include_router(user.router, prefix="")
app.include_router(match.router, prefix="")

#create_tables()  # 创建数据库表


@app.get("/a")
def read_root():
    return {"Hello": "World"}


@app.get("/")
def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request, "name": "Alice77"})

@app.get("/login")
def home(request: Request):
    return templates.TemplateResponse("login.html")


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        filename='app.log')
    app.mount("/static", StaticFiles(directory="static"), name="static")

    #start_background_tasks()  # 启动后台任务（如守护线程）
    uvicorn.run(app, host="0.0.0.0", port=8000)